/**
 * bookmark
 * @author wuji
 * @date 2020-07-19
 */
const {db} = require("../db/db.js");

/**
 * Select Bookmark By Id
 * @param {Integer} id 
 */
 exports.selectBookMarkById = function(id, cb) {
    db.get("select id, parent_id as parentId, type, title, url, icon, orders, hidden from tb_bookmark where id = ?", id, cb);
}

/**
 * Select All Bookmarks
 */
exports.selectBookMarkAll = function(cb) {
    db.all("select id, parent_id as parentId, type, title, url, icon, orders, hidden from tb_bookmark", cb);
}

/**
 * @param {Integer} parent_id 
 */
exports.selectBookMarksByparentId = function(parentId, cb) {
    db.all("select id, parent_id as parentId, type, title, url, icon, orders, hidden from tb_bookmark where parent_id = ?", parentId, cb);
}

/**
 * Add Bookmark
 * @param {Object} bookmark 
 */
exports.addBookMark = function(bookmark, cb) {
    db.run("insert into tb_bookmark (parent_id, type, title, url, icon, orders, hidden) values (?,?,?,?,?,?,?)", 
        [bookmark.parentId, bookmark.type, bookmark.title, bookmark.url, bookmark.icon, bookmark.orders, bookmark.hidden], cb);
}

/**
 * Delete Bookmark
 * @param {Integer} id 
 */
exports.deleteBookMarkById = function(id, cb) {
    db.run("delete from tb_bookmark where id = ? or parent_id = ?", [id, id], cb);
}

/**
 * Update Bookmark
 * @param {Object} bookmark 
 */
exports.updateBookMark = function(bookmark, cb) {
    db.run("update tb_bookmark set parent_id = ?, type = ?, title = ?, url = ?, icon = ?, orders = ?, hidden = ? where id = ?",
        [bookmark.parentId, bookmark.type, bookmark.title, bookmark.url, bookmark.icon, bookmark.orders, bookmark.hidden, bookmark.id], cb);
}
